<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	/*
 *      Contact.php
 *      
 *      Copyright 2011 godfather.typhoon.tycoon@banme <chungnguyenhuu911@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 *      
 *      
 */

	class  Contact   extends  CI_Controller{
		
		function  Contact  (){
			//parent::CI_Controller();
			
			}
		function __construct(){
			parent::__construct();
			$param = array('lang'=>'vietnamese');
			$this->load->library('recaptcha',$param);
			$this->lang->load('recaptcha','vietnamese');
			$this->lang->load('messages','vietnamese');
			$this->lang->load('form_validation','vietnamese');
		}
		
		function index(){
			
			
			/*
			test
			*/
			$this->load->helper('recaptcha');
			$data['contact'] = $this->findContact();
			
			/*
			 * Validate data
			 * 
			 */ 
			$this->form_validation-> set_rules('name', $this->lang->line('lang.i18n.messages.contact.name'), 'required|xss_clean');
			$this->form_validation-> set_rules('address', $this->lang->line('lang.i18n.messages.contact.address'), 'xss_clean');
			$this->form_validation-> set_rules('email', $this->lang->line('lang.i18n.messages.contact.email'), 'required|valid_email|xss_clean');
			$this->form_validation-> set_rules('phone', $this->lang->line('lang.i18n.messages.contact.phone'), 'xss_clean');
			$this->form_validation-> set_rules('content', $this->lang->line('lang.i18n.messages.contact.comment'), 'required|xss_clean');
			$data['captcha'] = recaptcha();
			if ($this->form_validation->run()) 
			{
				
				$name = $this->input->post('name');
				$email = $this->input->post('email');
				$address = $this->input->post('address');
				$phone = $this->input->post('phone');
				$content = $this->input->post('content');
				
				$this->insert($name, $address, $email, $phone, $content);
				$data = array();
				$data['message'] = "Gửi thành công";
				//$this->load->view('guest/contact', $data);
				$data['main_content'] = 'guest/contact';
				$this->load->view('templates/guest/template', $data);
				return;
			}
			else
			{
			//$data['main_content'] = 'guest/contact';
				$data['main_content'] = 'guest/contact';
				$this->load->view('templates/guest/template', $data);
				return;
			}
		}
		function check_captcha($val) {
		  if ($this->recaptcha->check_answer($this->input->ip_address(),$this->input->post('recaptcha_challenge_field'),$val)) {
			return TRUE;
		  } else {
			$this->form_validation->set_message('check_captcha',$this->lang->line('recaptcha_incorrect_response'));
			return FALSE;
		  }
		}
		
		function loadContact(){
			require_once('module.php');
			$module = new Module();
			$module->findByID(2);
			print_r($module);
			
		}
		function findContact(){
			$sql = "SELECT `id`,`name`, `name_en`, `content`, `content_en` FROM `module` WHERE `id` = ? limit 1";
			$query = $this->db->query($sql, array('id'=>2)); 
			$data = array();
			if($query->num_rows() > 0){
				$data = $query->row_array();
			}
			$query->free_result();
			return $data;
		}
		function insert($name = "", $address = "", $email = "", $phone = "", $content = ""){
			$sql = "INSERT INTO  `contact` (name, address, email, phone, content) VALUES (?, ?, ?, ?, ?)";
			
			$query = $this->db->query($sql, array($name, $address, $email, $phone, $content));
			}
	}
?>
